package com.ptszyxx.popose.module.main.home.vm;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.event.HomeTabFinishEvent;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YLoginUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.enums.ChatCallEnum;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.home.HomeAccountEntity;
import com.ysg.http.data.entity.home.HomeResult;
import com.ysg.http.data.entity.login.LoginResult;
import com.ysg.http.data.entity.mine.MineWalletEntity;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YMD5Utils;
import com.ysg.utils.YPhoneUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YTimeUtil;
import com.ysg.utils.YToastUtil;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class HomeVM extends BaseViewModel<CommonRepository> {
    public boolean noMoreCity, noMoreNear, noMoreOnline, noMoreAccount;
    public ObservableField<String> sexOnline = new ObservableField<>("1");
    public int position;
    public HomeResult currentItem;
    public MineWalletEntity walletEntity;

    public HomeVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.home);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRandomUserEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onVideoMatchCommand = new BindingCommand(()-> jumpCallMatch(ChatCallEnum.VIDEO));
    public BindingCommand onAudioMatchCommand = new BindingCommand(()-> jumpCallMatch(ChatCallEnum.AUDIO));
    public BindingCommand onRankCommand = new BindingCommand(()-> jumpHomeRank());
    public BindingCommand onSearchCommand = new BindingCommand(()-> YActivityUtil.getInstance().jumpHomeSearch(this));

    //TODO 3.异步通知处理=====================
    public Disposable disposable;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposable = RxBus.getDefault().toObservable(HomeTabFinishEvent.class)
                .subscribe(new Consumer<HomeTabFinishEvent>() {
                    @Override
                    public void accept(HomeTabFinishEvent event){
                        if (event != null){
                            switch (event.getTabEnum()){
                                case city:
                                    noMoreCity = event.isNoMore();
                                    break;
                                case near:
                                    noMoreNear = event.isNoMore();
                                    break;
                                case online:
                                    noMoreOnline = event.isNoMore();
                                    break;
                                case account:
                                    noMoreAccount = event.isNoMore();
                                    break;
                            }
                            uc.onRefreshEvent.setValue(event.isNoMore());
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposable);
    }

    //=========================================接口：start===========================================
    /**
     * 今日缘分
     */
    public void requestRandomUserList(){
        boolean b = YSPUtils.getInstance().getBoolean(YTimeUtil.getToday()+YSPUtils.getInstance().getUserId(), false);
        if (b){
            return;
        }
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.homeRandomUserList(map), this,
                        new OnSuccessListResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserResult> response) {
                                uc.onRandomUserEvent.setValue(response.getData());
                                requestWallet();
                            }
                        });
    }
    /**
     * 今日缘分-一键搭讪
     */
    public void requestSelectAccost(String ids){
        String[] split = ids.split(",");
        for (int i = 0; i < split.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("userId", split[i]);
            int finalI = i;
            HttpUtils.getInstance()
                    .data(model.homeSelectAccost(map), this,
                            new OnSuccessResult() {
                                @Override
                                public void onSuccessResult(BaseResponse response) {
                                    if (finalI == 0){
                                        YToastUtil.showShort(response.getMsg());
                                        YSPUtils.getInstance().put(YTimeUtil.getToday()+YSPUtils.getInstance().getUserId(), true);
                                    }
                                }
                            });
        }
    }

    /**
     * 钱包
     */
    public void requestWallet(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance().data(model.wallet(map), this, response -> {
                    walletEntity = (MineWalletEntity)response.getData();
                });
    }

    /**
     * 登录
     */
    public void requestLogin(HomeAccountEntity entity){
        Map<String, String> map = new HashMap<>();
        map.put("loginname", entity.getPhone());
        map.put("password", YMD5Utils.get(entity.getPassword()));
        map.put("loginType", "1");
        map.put("mobileSpec", YPhoneUtil.getModel());

        HttpUtils.getInstance()
                .data(model.login(map), this,
                        new OnSuccessResult<LoginResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<LoginResult> response) {
                                YLoginUtil.getInstance().login(HomeVM.this, response.getData());
                            }
                        });
    }


    //=========================================接口：end=============================================

    /**
     * 个人详情
     */
    public void jumpPersonDetail(HomeResult item){
        YActivityUtil.getInstance().jumpUserDetail(this, String.valueOf(item.getUserid()));
    }

    /**
     * 聊天
     */
    public void jumpChat(HomeResult item){
        YActivityUtil.getInstance().jumpChat(this, item.getTexcode()+"", item.getUsernick(), item.getUsericon());
    }

    /**
     * 心动
     */
    public void heart(HomeResult item, int position){
        this.position = position;
        this.currentItem = item;
    }

    /**
     * 通话匹配
     */
    private void jumpCallMatch(ChatCallEnum callEnum) {
        YActivityUtil.getInstance().jumpHomeCallMatch(this, callEnum);
    }
    /**
     * 排行榜
     */
    private void jumpHomeRank() {
        YActivityUtil.getInstance().jumpHomeRank(this);
    }





}
